package com.inossem.warehousestorage_inossem.activity.change_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.changeStorage.ChangeStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageMaterielListBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.changeStorage.ChangeStorageMaterielListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.ChangeStorageUtils;

import java.util.ArrayList;
import java.util.List;

import static com.inossem.warehousestorage_inossem.utils.ChangeStorageUtils.getChangeStorageBean;

//转储管理物料列表
public class ChangeStorageMaterielListActivity extends BaseActivity {

    private TextView title, nextStep;

    private PullToRefreshLayout refresh;

    private PullableListView materielList;

    private List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> allData;

    private ChangeStorageListAdapter adapter;

    private String mCondition, mFactoryCode, mInventoryAddressCode, mBusinessType, mSpecialInventory;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_storage_materiel_list);
        initView(ChangeStorageMaterielListActivity.this);
        initData(ChangeStorageMaterielListActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        getDataFromLocal(ChangeStorageMaterielListActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        materielList = (PullableListView) activity.findViewById(R.id.materielList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.materielListRefresh);
    }

    private void initData(Activity activity) {
        title.setText("添加物料");
        ActivityUtils.initBackAndFunction(activity, "管理", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        ChangeStorageBean changeStorageBean = getChangeStorageBean(activity);
        mFactoryCode = changeStorageBean.getFactoryCode();
        mInventoryAddressCode = changeStorageBean.getInventoryAddressCode();
        mBusinessType = changeStorageBean.getBusinessType();
        mSpecialInventory = changeStorageBean.getSpecialInventory();
        materielList.setPullUpFlag(false);
        materielList.setPullDownFlag(false);
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (check(activity)) {
                    Intent intent = new Intent(activity, ChangeStorageAddOperatorActivity.class);
                    startActivity(intent);
                }
            }
        });
        setmScanListener((String scanResult, int length) -> {
            mCondition = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity, mCondition, mFactoryCode, mInventoryAddressCode, mBusinessType, mSpecialInventory);
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
    }

    private boolean check(Context context) {
        int confirmNumber = ChangeStorageUtils.getChangeStorageMaterielNumber(context);
        if (confirmNumber == 0) {
            Utils.middleToast(context, "请至少完善一个行项目");
            return false;
        }
        return true;
    }

    private void addChangeStorageMaterielListData(Context context, List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> materielList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new ChangeStorageListAdapter(context, materielList, mCondition, new ChangeStorageMaterielListener() {
                @Override
                public void onNoDoubleClickListener(String condition) {
                    String checkResult = check(condition);
                    if (checkResult == null) {
                        mCondition = condition;
                        getData(context, mCondition, mFactoryCode, mInventoryAddressCode, mBusinessType, mSpecialInventory);
                    } else {
                        Utils.middleToast(context, checkResult);
                    }
                }

                @Override
                public void deleteMaterielItem(String id, String materielCode) {
                    DialogUtils.getConfirmDialog(context, "确认删除物料编码为" + materielCode + "的物料?", new DialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onConfirm(Dialog dialog) {
                            dialog.cancel();
                            ChangeStorageUtils.deleteMaterielById(context, id);
                            ChangeStorageBean bean = ChangeStorageUtils.getChangeStorageBean(context);
                            allData = bean.getMaterielList() == null ? new ArrayList<>() : bean.getMaterielList();
                            addChangeStorageMaterielListData(context, ActivityUtils.getShowData(ChangeStorageUtils.getChangeStorageBean(context).getMaterielList(), 0), listView, true);
                        }
                    }).show();
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private String check(String saleNumber) {
        String result = null;
        if (TextUtils.isEmpty(saleNumber)) {
            result = "销售单号不可以为空";
        }
        return result;
    }

    private void getData(Context context, String condition, String factoryCode, String inventoryAddressCode, String businessType, String specialInventory) {
        ChangeStorageMaterielListRequest.request(context, condition, factoryCode, inventoryAddressCode, businessType, specialInventory,
                new GsonBeanListener<ChangeStorageMaterielListBean>() {
                    @Override
                    public void onSuccess(ChangeStorageMaterielListBean bean) {
                        if (bean.getData().getMaterielList().isEmpty()) {
                            Utils.middleToast(context, "未查到数据");
                        }
                        ChangeStorageBean changeStorageBean = ChangeStorageUtils.getChangeStorageBean(context);
                        allData = addMateriel(changeStorageBean.getMaterielList(), bean.getData().getMaterielList());
                        changeStorageBean.setMaterielList(allData);
                        ChangeStorageUtils.saveChangeStorageBean(context, changeStorageBean);
                        addChangeStorageMaterielListData(context, ActivityUtils.getShowData(allData, 0), materielList, true);
                    }
                });
    }

    private void getDataFromLocal(Context context) {
        List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> materielBeanList = getChangeStorageBean(context).getMaterielList();
        if (materielBeanList == null || materielBeanList.isEmpty()) {
            allData = new ArrayList<>();
        } else {
            allData = materielBeanList;
        }
        addChangeStorageMaterielListData(context, allData, materielList, true);
    }

    private List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> addMateriel(List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> currentList, List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> addList) {
        if (currentList == null || currentList.isEmpty()) {
            return addList;
        } else {
            List<ChangeStorageMaterielListBean.DataBean.MaterielListBean> list = new ArrayList<>();
            for (int i = 0; i < addList.size(); i++) {
                ChangeStorageMaterielListBean.DataBean.MaterielListBean addItem = addList.get(i);
                for (int j = 0; j < currentList.size(); j++) {
                    ChangeStorageMaterielListBean.DataBean.MaterielListBean currentItem = currentList.get(i);
                    if (currentItem.getId().equals(addItem.getId())) {
                        list.add(addItem);
                        break;
                    }
                }
            }
            addList.removeAll(list);
            currentList.addAll(0, addList);
            return currentList;
        }
    }

    public interface ChangeStorageMaterielListener {
        void onNoDoubleClickListener(String condition);

        void deleteMaterielItem(String id, String materielCode);
    }

}
